﻿using System;
using System.Collections.Generic;
using System.Data.Common;

namespace Mind.Data
{
    /// <summary>
    /// Classe para acesso as conexões com diferentes servidores de dados.
    /// </summary>
    public class DataAccessManager
    {
        private static DataAccess _Current;
        private Dictionary<String, DataAccess> _DataAccessConnections;
        public const string DEFAULT_CONNECTION_NAME = "DefaultConnectionString";
        private static DataAccessManager _Connections;

        /// <summary>
        /// Busca as conexões configuradas na sessão connectionStrings
        /// do App.Conf
        /// </summary>
        public static DataAccessManager Connections
        {
            get
            {
                
                if (_Connections == null)
                    _Connections = new DataAccessManager();
                return _Connections;
            }
        }

        /// <summary>
        /// Busca o nome da conexão default configurada no App.Conf na sessão
        /// appSettings com o nome de DefaultConnectionString;
        /// 
        /// A configuração da conexão deve estar na sessão connectionStrings
        /// do App.Conf
        /// </summary>
        public static DataAccess Current
        {
            get
            {
                if (DataAccessScope.Current != null)
                    return DataAccessScope.Current.DataAccess;

                if (_Current == null)
                    _Current = Connections[DEFAULT_CONNECTION_NAME];

                return _Current;
            }

            set
            {
                _Current = value;
            }


        }


        public static DataAccess CreateDataAccess(string name, DataAccessProvider provider, string connectionString)
        {
            if (Connections._DataAccessConnections.ContainsKey(name))
                throw new InvalidOperationException(String.Format("Already exists a DataAccess with this name '{0}'.", name));

            DataAccess data = new DataAccess(name, provider, connectionString);
            Connections._DataAccessConnections[name] = data;
            return data;

        }

        public static DataAccess CreateDataAccess(string name, DbProviderFactory provider, string connectionString)
        {
            if (Connections._DataAccessConnections.ContainsKey(name))
                throw new InvalidOperationException(String.Format("Already exists a DataAccess with this name '{0}'.", name));

            DataAccess data = new DataAccess(name, provider, connectionString);
            Connections._DataAccessConnections[name] = data;
            return data;

        }

        /// <summary>
        /// Busca as conexões configuradas na sessão connectionStrings
        /// do App.Conf
        /// </summary>
        public DataAccess this[string connectionName]
        {
            get
            {
                return GetConnection(connectionName);
            }
        }

        /// <summary>
        /// Busca as conexões configuradas na sessão connectionStrings
        /// do App.Conf
        /// </summary>
        public DataAccess GetConnection(string connectionName)
        {
            DataAccess result = null;
            if (_DataAccessConnections.ContainsKey(connectionName))
            {
                result = _DataAccessConnections[connectionName];
            }
            
            if (result == null)
            {
                result = new DataAccess(connectionName);
                _DataAccessConnections[connectionName] = result;
            }
            
            return result;
        }


        private DataAccessManager()
        {
            _DataAccessConnections = new Dictionary<String, DataAccess>();
            
        }


    }

}


